package com.ychs.servlet;


import com.sun.org.apache.bcel.internal.generic.DUP;
import com.ychs.entity.College;
import com.ychs.entity.Credits;
import com.ychs.service.CreditsService;
import com.ychs.service.IBaseService;
import com.ychs.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/22
 */
@WebServlet(value = "/CreditsServlet")
public class CreditsServlet extends BaseServlet {
    private IBaseService<Credits> service = new CreditsService();
    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter credits add method");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        String grade = request.getParameter("grade");
        String collegeId = request.getParameter("college");
        String achieveTime = request.getParameter("achieveTime");
        String score = request.getParameter("score");
        String reason = request.getParameter("reason");
        String enterMan = request.getParameter("enterMan");
        String remark = request.getParameter("remark");

        Credits credits = new Credits();
        credits.setName(name);
        credits.setCode(code);
        credits.setGrade(Integer.parseInt(grade));
        College college = new College();
        college.setId(Integer.parseInt(collegeId));
        credits.setCollege(college);
        credits.setAchieveTime(DateUtil.StringToDate(achieveTime));
        //credits.setAchieveTime(new Date());
        credits.setScore(Integer.parseInt(score));
        credits.setReason(reason);
        credits.setEnterMan(enterMan);
        credits.setRemark(remark);
        boolean result = service.add(credits);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter credits search method");
        Map<String, Object> conditions = new HashMap<>();

        /*String searchCode = request.getParameter("searchCode");
        String searchName = request.getParameter("searchName");*/
        String searchDateRange = request.getParameter("searchDateRange");
        String searchCode = request.getParameter("searchCode");
        logger.info(searchCode);
        if (searchDateRange != null && !"".equals(searchDateRange)) {
            String beginDate = searchDateRange.substring(0, 7);
            String endDate = searchDateRange.substring(10, 17);
            conditions.put("beginDate", DateUtil.StringToDate(beginDate));
            conditions.put("endDate", DateUtil.StringToDate(endDate));
        }
        if (searchCode != null && !"".equals(searchCode)) {
            conditions.put("code", searchCode);
        }

        int count = service.searchTotalNumber(conditions);
        List<Credits> creditsList = service.search(conditions);
        String json = json(count, creditsList);
        logger.info(json);
        response.getWriter().write(json);
    }

    @Override
    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
